// 1.Promise
// 模拟红灯亮
function red() {
    console.log('red')
}

//模拟绿灯亮
function green() {
    console.log('green')
}

//模拟黄灯亮
function yellow() {
    console.log('yellow')
}
// 返回一个Promise的函数 Promise内模拟路灯等待
const task = (timer, light) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (light === 'red') {
                red()
            } else if (light === 'green') {
                green()
            } else if (light === 'yellow') {
                yellow()
            }
            // 触发then的回调
            resolve()
        }, timer)
    })
}


const step = () => {
    task(2000, 'red')
        .then(() => task(500, 'yellow'))
        .then(() => task(1000, 'green'))
        .then(() => task(500, 'yellow'))
        .then(step)
}
step()

// 2.async await   这个时间设置更合理
const aa = async () => {
    await task(500,'red')
    await task(2200,'yellow')
    await task(500,'green')
    await task(2200,'yellow')
    await aa()
}
aa()


